﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using RCREEE.Library.Entities;

namespace RCREEE.Library
{
    public static partial class DataManager
    {
        public static DataTable GetColumnNames(string TableName)
        {
            _Command.CommandText = "SP_GetTableColumns";
            _Command.CommandType = CommandType.StoredProcedure;

            _Command.Parameters.Clear();
            _DataSet.Tables.Clear();

            _Command.Parameters.Add("@TableName", SqlDbType.NVarChar).Value = TableName;
            _Adapter.SelectCommand = _Command;

            try
            {
                _Adapter.Fill(_DataSet);
            }
            catch (Exception exc)
            {
                CloseConnection();
            }

            if (_DataSet != null && _DataSet.Tables[0].Rows.Count > 0)
            {
                return _DataSet.Tables[0];
            }

            return null;
        }

        public static bool AddColumn(string ColumnName, string TableName)
        {
            _Command.CommandText = "SP_AddNewColumn";
            _Command.CommandType = CommandType.StoredProcedure;

            _Command.Parameters.Clear();

            _Command.Parameters.Add("@ColumnName", SqlDbType.NVarChar).Value = ColumnName;

            if (TableName == "StrategicObjectives")
                _Command.Parameters.Add("@ColumnDataType", SqlDbType.NVarChar).Value = "[float]";
            else
                _Command.Parameters.Add("@ColumnDataType", SqlDbType.NVarChar).Value = "[nvarchar](100)";

            _Command.Parameters.Add("@TableName", SqlDbType.NVarChar).Value = TableName;

            try
            {
                OpenConnection();

                _Command.ExecuteNonQuery();

                CloseConnection();

                return true;
            }
            catch (Exception exc)
            {
                CloseConnection();
                return false;
            }
        }

        public static bool RemoveColumn(string ColumnName, string TableName)
        {
            _Command.CommandText = "SP_RemoveColumn";
            _Command.CommandType = CommandType.StoredProcedure;

            _Command.Parameters.Clear();

            _Command.Parameters.Add("@ColumnName", SqlDbType.NVarChar).Value = ColumnName;
            _Command.Parameters.Add("@TableName", SqlDbType.NVarChar).Value = TableName;

            try
            {
                OpenConnection();

                _Command.ExecuteNonQuery();

                CloseConnection();

                return true;
            }
            catch (Exception exc)
            {
                CloseConnection();
                return false;
            }
        }


        
    }
}
